package com.ycd.Servlet;

import com.ycd.Entity.UserEntity;
import com.ycd.Service.UserEntityService;
import com.ycd.Service.impl.UserEntityServiceImpl;
import com.ycd.Util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Description 注册Servlet
 * @Author ycd
 * @Date : 2022/3/17 9:23
 * @Version : 1.0
 */
@WebServlet("/reg")
public class RegHtmlServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //规范字符集
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String userAcct = req.getParameter("userAcct");
        String userPwd = req.getParameter("userPwd");

        UserEntityService userEntityService=new UserEntityServiceImpl();

        //判定注册账号密码是否为空
        if (userAcct.equals("") && userPwd.equals("")) {
            //跳转到登录页面
            req.getRequestDispatcher("login.h?type=5").forward(req,resp);
        }else {
            UserEntity userentity = new UserEntity();
            userentity.setAcct(userAcct);
            userentity.setPwd(MD5Util.toMD5(userPwd));
            //默认值
            userentity.setName("余传镝");
            if (userEntityService.getAddReg(userAcct)) {
                //注册失败
                req.getRequestDispatcher("login.h?type=5").forward(req,resp);
            } else {
                //注册成功，添加用户
                userEntityService.addUser(userentity);
                req.getRequestDispatcher("login.h?type=4").forward(req,resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);

    }
}
